package com.kotlin.heimaplayer.base


import android.graphics.Color
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.kotlin.heimaplayer.R
import com.kotlin.heimaplayer.adapter.BaseListAdapter
import com.kotlin.heimaplayer.adapter.HomeAdapter
import com.kotlin.heimaplayer.model.HomeItemBean
import com.kotlin.heimaplayer.presenter.impl.HomePresenterImpl
import kotlinx.android.synthetic.main.fragment_list.*

/**
 * 上拉刷新，下拉加载更多，列表
 */
abstract class BaseListFragment<RESPONSE,ITEMBEAN,ITEMVIEW:View>: BaseFragment(),BaseView<RESPONSE> {

    override fun onDestroy() {
        super.onDestroy()
        // 解绑p层和v层
//        presenter.destoryView()
    }

    override fun onError(message: String?) {
        myToast("加载数据失败$message")

    }

    override fun loadSuccess(list: RESPONSE?) {
        // 隐藏刷新控件

        adapter.updateList(getList(list))
        println("刷新")
        refreshLayout?.let{
            refreshLayout.isRefreshing = false
        }

    }

    override fun loadMore(list: RESPONSE?) {
        // 隐藏刷新控件
        println("加载更多")
        adapter.loadMore(getList(list))
    }


    val adapter by lazy { getSpecialAdapter() }


    val presenter by lazy { getSpecialPresenter() }


    override fun initView(): View? {
        return View.inflate(context, R.layout.fragment_list, null)
    }

    override fun initListener() {
        recycleView.layoutManager = LinearLayoutManager(context)
        // 适配

        recycleView.adapter = adapter

        // 初始化刷新控件
        refreshLayout.setColorSchemeColors(Color.RED, Color.YELLOW, Color.GREEN)
        // 刷新监听
        refreshLayout.setOnRefreshListener {
            presenter.loadDatas()
        }

        // 监听滚动
        recycleView.addOnScrollListener(object : RecyclerView.OnScrollListener(){


            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                // 判断是不是停止状态
                if(newState == RecyclerView.SCROLL_STATE_IDLE){
                    //是否最后一条已经显示
                    val layoutManager = recyclerView.layoutManager
                    if(layoutManager is LinearLayoutManager){
                        val manager: LinearLayoutManager = layoutManager
                        val lastPosition = manager.findLastVisibleItemPosition()
                        if(lastPosition == adapter.itemCount - 1){
                            // 最后一条已经显示
                            val page = ((adapter.itemCount-1)/10)+1
                            println(adapter.itemCount)
                            println(page)
                            presenter.loadMore(page)
                        }
                    }
                }
            }

        })
    }

    override fun initData() {
        presenter.loadDatas()
    }

    /**
     * 加载更多数据
     */

    // 获取适配器adapter
    abstract fun getSpecialAdapter():BaseListAdapter<ITEMBEAN,ITEMVIEW>

    abstract fun getSpecialPresenter():BaseListPresenter

    abstract fun getList(response: RESPONSE?):List<ITEMBEAN>?



}